package service;

import java.util.ArrayList;
import java.util.List;


public class MessageQueue {
        List<Message> messages = new ArrayList<Message>();
        private boolean stop = false; 
        
        public synchronized void add(String message, String reciever) {
                messages.add(new Message(reciever, message));
                notifyAll(); 
        }
        
        public synchronized Message getNextMessage(){
                try{
                        while (messages.isEmpty()){
                                if (stop)
                                        return null;
                                
                                wait();
                        } 
                } catch (InterruptedException e){
                        e.printStackTrace();
                }
                
                return messages.remove(0);
        }
        
        public synchronized void stop(){
                stop = true;
                notifyAll(); 
        }
        
        public List<Message> getAllMessages() {
                return messages;
        }
}